<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"> 
    	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    	<meta name="apple-mobile-web-app-capable" content="yes">  
    	<meta name="apple-mobile-web-app-status-bar-style" content="black">  
		<title></title>
		<script src="js/mui.min.js"></script>
		<script src="js/store.min.js"></script>
   	    <link href="css/mui.min.css" rel="stylesheet"/>
   	    <style type="text/css">
   	    .mui-content {
    		margin-top: 10px;
    	}
    	.mui-input-group .mui-input-row {
    		height:50px;
    	}
    	.mui-input-group .mui-button-row{
    		height:50px;
    		padding-top:8px;
    	}
   	    </style>
	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">K-V</h1>
			<button class="mui-active-back mui-btn mui-btn-blue mui-btn-link mui-pull-left" id="back">
			<span class="mui-icon mui-icon-left-nav"></span>首页
		</button>
	</header>
	<div class="mui-content">
		<form class="mui-input-group">
					<div class="mui-input-row">
						<label>车牌</label>
						<input type="text" placeholder="车牌号" id="ofo_key" />
					</div>
					<div class="mui-input-row">
						<label>密码</label>
						<input type="text"  placeholder="密码" id="ofo_value"/>
					</div>
					<div class="mui-button-row">
						<button type="button" class="mui-btn mui-btn-primary" id="save">存储</button>&nbsp;&nbsp;
						<button type="button" class="mui-btn mui-btn-danger" id="get">取出</button>
					</div>
				</form>
	</div>
	</body>
	<script type="text/javascript">
	var json;
	
	document.addEventListener( "plusready", onPlusReady, false );
	
	function onPlusReady() {
		plus.io.requestFileSystem( plus.io.PUBLIC_DOCUMENTS, function( fs ) {
			fs.root.getFile("ofo.json",{create:true,exclusive:false}, function(fileEntry){
				fileEntry.file( function(file){
					var fileReader = new plus.io.FileReader();
					fileReader.readAsText(file, 'utf-8');
					fileReader.onloadend = function(evt) {
						json=evt.target.result;
						console.log(json);
					}
				} );
			});
		}, function ( e ) {
			mui.toast( "Request file system failed: " + e.message );
			} );

	}
	
	function jsonWriter(newJson){
		plus.io.requestFileSystem( plus.io.PUBLIC_DOCUMENTS, function( fs ) {
			fs.root.getFile("ofo.json",{create:false,exclusive:false}, function(fileEntry){
				fileEntry.createWriter( function ( writer ) {
						writer.onwrite = function ( e ) {
							plus.console.log( "Write data success!" );
						};
						writer.seek( 0 );
						writer.write( newJson);
					}, function ( e ) {
					mui.toast( "Resolve file URL failed: " + e.message );
				} );
			});
		}, function ( e ) {
			mui.toast( "Request file system failed: " + e.message );
		} );
	}
	
	mui.plusReady(function(){
		document.getElementById("save").addEventListener("tap",function(){
			var ofo_key=document.getElementById("ofo_key").value.trim();
			var ofo_value=document.getElementById("ofo_value").value.trim();
			if(!ofo_key && !ofo_value){
				mui.toast("key-value不能为空");
				return ;
			}
			var obj;
			if(!json){
				obj=new Object();
			}else{
				obj=JSON.parse(json);
			}
			obj[ofo_key]=ofo_value;
			var str =JSON.stringify(obj);
			json =str;
			jsonWriter(str);
			mui.toast("成功保存");
		});
			
		document.getElementById("get").addEventListener("tap",function(){
			var ofo_key=document.getElementById("ofo_key").value.trim();
			if(!ofo_key){
				mui.toast("key不能为空");
				return ;
			}
			var obj=JSON.parse(json);
			var value =obj[ofo_key];
			if(!value){
				mui.toast("不存在该车牌");
				return ;
			}	
			document.getElementById("ofo_value").value=value;
		///	mui.toast(value);
		});	
		
		document.getElementById("back").addEventListener("tap",function(){
			window.location.href="index.html";
		});		
    })
	</script>
</html>
